<?php

namespace app\admin\controller;

use app\admin\model\FileUpload;
use app\admin\service\FileUploadService;
use app\admin\validate\FileValidate;
use common\apidoc\Definitions;
use common\enum\ResultCode;
use hg\apidoc\annotation as Apidoc;
use support\Request;
use support\Response;
use support\Container;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

#[Apidoc\Title("文件管理")]
#[Apidoc\Sort(999)]
class FileUploadController extends BaseController
{
    private FileUploadService $service;
    protected FileValidate $validate;

    public function __construct()
    {
        $this->service = Container::get(FileUploadService::class);
        $this->validate = Container::get(FileValidate::class);
    }

    #[Apidoc\Title("文件列表")]
    #[Apidoc\Url("admin/file/index")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query(ref: [Definitions::class, "pagingQueryParam"])]
    #[Apidoc\Query("name", type: "string", desc: "文件名")]
    #[Apidoc\Query("original_name", type: "string", desc: "原始文件名")]
    #[Apidoc\Query("status", type: "int", desc: "状态:0=禁用,1=启用")]
    #[Apidoc\Returned(ref: [Definitions::class, "pagingReturnParam"])]
    #[Apidoc\Returned("list", type: "array", ref: FileUpload::class, desc: "文件列表")]
    public function index(Request $request): Response
    {
        $params = [
            'page' => $request->get('page', 1),
            'limit' => $request->get('limit', 10),
            'name' => $request->get('name', ''),
            'original_name' => $request->get('original_name', ''),
            'status' => $request->get('status', ''),
        ];
        if (!$this->validate->check($params, 'index')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getList($params);
        return $this->success($result);
    }

    #[Apidoc\Title("文件详情")]
    #[Apidoc\Url("admin/file/info")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query("id", type: "int", require: true, desc: "文件ID")]
    #[Apidoc\Returned(type: "object", ref: FileUpload::class, desc: "文件详情")]
    public function info(Request $request): Response
    {
        $id = $request->get('id');
        if (!$this->validate->check(['id' => $id], 'info')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getInfo($id);
        return $this->success($result);
    }

    #[Apidoc\Title("上传文件")]
    #[Apidoc\Url("admin/file/upload")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("file", type: "file", require: true, desc: "上传的文件")]
    #[Apidoc\Returned(type: "object", ref: FileUpload::class, desc: "文件信息")]
    public function upload(Request $request): Response
    {
        $file = $request->file('file');
        if (!$file) {
            throw new BadRequestHttpException('请选择要上传的文件');
        }
        $result = $this->service->upload($file);
        return $this->success($result, ResultCode::CREATE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("删除文件")]
    #[Apidoc\Url("admin/file/delete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "文件ID")]
    public function delete(Request $request): Response
    {
        $id = $request->post('id');
        if (!$this->validate->check(['id' => $id], 'delete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->delete($id);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("批量删除文件")]
    #[Apidoc\Url("admin/file/batchDelete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("ids", type: "array", require: true, desc: "文件ID数组")]
    public function batchDelete(Request $request): Response
    {
        $ids = $request->post('ids');
        if (!$this->validate->check(['ids' => $ids], 'batchDelete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->batchDelete($ids);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }
}